<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\model\user;

use think\Model;

/**
 * 会员用户模型
 * @package app\user_center\model
 */
class Member extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_uc_member';
    //设置主键
    protected $pk = 'uid';

    /**
     * 获取用户信息(联合查询)
     * @param int $uid 用户id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function getUserinfoByUid($uid){
        $map = [];
        $map['member.uid'] = $uid;
        //$map['getUcAttestation.attestation_status'] = 1;
        //$map['getUcAttestationArtist.artist_status'] = 1;
        $info = $this -> withJoin('getUcMemberCount,getUcMemberAuthtoken,getUcMemberMobile,getUcMemberWechat,getUcAttestation,getUcAttestationArtist', 'LEFT')->where($map)->find();
        if(!empty($info)){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    public function getUserinfoByUseruid($user_uid){ 
        $info = $this -> cache(true)->find($user_uid);
        if(!empty($info)){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }


    /**
     * 新增用户(不包含绑定信息)
     * @param string $nick_name 用户昵称
     * @param int    $sex 性别 0不显示 1男 2女
     * @param string $autograph 个性签名
     * @param string $avatar_url 头像url地址
     * @param datetime $birthday 生日日期 
     * @param int $f_uid 推荐人uid 
     * @param int $create_time 注册时间
     * @param string $balance 余额
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function addUser($nick_name, $sex, $autograph, $avatar_url, $birthday, $f_uid = '', $create_time = '', $balance = 0, $member_status=1 ){
        self::startTrans();//启动事务
        try {
            //会员主表
            $data['nick_name']  	= $nick_name;
            $data['sex']        	= $sex;
            $data['autograph']  	= $autograph;
            $data['avatar_url'] 	= $avatar_url;
            $data['member_status']  = $member_status;
            $data['f_uid']   		= $f_uid;
			$data['birthday'] 		= $birthday;
			$data['balance'] 		= $balance;
			if($create_time != '')$data['create_time'] 	= $create_time;
			
            $res1 = $this -> isUpdate(false) ->data($data, true) -> save();//添加  thinkphp主键bug
     
            //会员统计表
            $res2 = model('user_center/UcMemberCount', 'model\user') -> addData($this->uid, $create_time);
            //会员权限表
            $res3 = model('user_center/UcMemberAuthtoken', 'model\user') -> addData($this->uid, $create_time);
            if($res1 > 0 && $res2 > 0 && $res3 > 0){
                self::commit();
                //$data = ['uid' => $this -> uid];
                return array_out(200, 'success', $this -> uid);
            }else{
                return array_out(-1001, 'error');
            }
        } catch (\Exception $e) {
            // 回滚事务
            self::rollback();
            return array_out(-9999, $e->getMessage());
        }
    }
	
	/**
     * 更新用户信息
     * @param string $uid 		用户id
     * @param string $nick_name 用户昵称
     * @param int    $sex 性别 0不显示 1男 2女
     * @param string $autograph 个性签名
     * @param string $avatar_url 头像url地址
     * @param datetime $birthday 生日日期 
     * @param datetime $member_status 会员状态
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function updateDataByUser($uid, $nick_name, $sex, $autograph, $avatar_url, $birthday, $member_status = -1, $title){
        $map = [];
        $map['uid'] = $uid;
		//会员主表
        $data = [];
        //$data['uid'] 	= $uid;
		if($nick_name) $data['nick_name'] 	= $nick_name;
		if($sex) $data['sex'] 		        = $sex;
		if($autograph) $data['autograph'] 	= $autograph;
		if($avatar_url) $data['avatar_url']	= $avatar_url.'?temp='.time();
        if($birthday) $data['birthday'] 	= $birthday;
        if($title) $data['title'] 	= $title;
        if(in_array($member_status, [0,1])) $data['member_status'] 	= $member_status;
        $res = $this ->update($data, $map);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	
	/**
     * 更新用户登录信息
     * @param int $uid 			 	用户id
     * @param int $last_login_ip 	用户ip
     * @param int $last_login_type	用户登录方式 1 手机验证码  2 微信  3 一键登录
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function updateUserLogin($uid, $last_login_type){
		$map = [];
		$map['last_login_ip']	= get_client_ip();//获取IP
		$map['last_login_type'] = $last_login_type;
		$map['last_login_time'] = time();
        $res = self::where('uid', $uid) ->update($map);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}

    /**
     * 获取用户信息（单表查询）
     * @param int $uid 用户id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function getDateByUid($uid, $cache = false){
        if($cache){
            $info =  $this->find($uid);
        }else{
            $info =  $this->cache(true)->find($uid);
        }
        
        if($info){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 获取用户列表数据
     * @param int $uid 用户id
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function getList(){
        $list = self::withJoin('getVideoPlayComment,getUcMemberAuthtoken,getUcMemberMobile,getUcMemberWechat', 'LEFT') -> order('create_time desc') -> paginate();
        return array_out(200, 'success', $list);
    }

	
    /**
     * 用户统计 关联载入（一对一）
     */
    public function getUcMemberCount(){
        return $this -> hasOne('app\user_center\model\user\UcMemberCount','uid', 'uid');
    }

    /**
     * 个人隐私 关联载入（一对一）
     */
    public function getUcMemberPrivacy(){
        return $this -> hasOne('app\user_center\model\user\UcMemberPrivacy','uid', 'uid');
    }

    /**
     * 用户权限 关联载入（一对一）
     */
    public function getUcMemberAuthtoken(){
        return $this -> hasOne('app\user_center\model\user\UcMemberAuthtoken','uid', 'uid');
    }

    /**
     * 用户手机 关联载入（一对一）
     */
    public function getUcMemberMobile(){
        return $this -> hasOne('app\user_center\model\user\UcMemberMobile','uid', 'uid');
    }

    /**
     * 用户微信 关联载入（一对一）
     */
    public function getUcMemberWechat(){
        return $this -> hasOne('app\user_center\model\user\UcMemberWechat','uid', 'uid');
    }
	
	/**
     * 苹果账户 关联载入（一对一）
     */
    public function getUcMemberApple(){
        return $this -> hasOne('app\user_center\model\user\UcMemberApple','uid', 'uid');
    }
    
    /**
     * 用户机构 关联载入（一对一）
     */
    public function getUcAttestation(){
        return $this -> hasOne('app\user_center\model\mcn\UcAttestation','uid', 'uid');
    }

    /**
     * 用户艺人 关联载入（一对一）
     */
    public function getUcAttestationArtist(){
        return $this -> hasOne('app\user_center\model\mcn\UcAttestationArtist','uid', 'uid');
    }

    /**
     * 用户关注 关联载入（一对多）
     */
    public function getUcMemberFollow(){
        return $this -> hasMany('app\user_center\model\user\UcMemberFollow','uid', 'uid');
    }
	
	/**
     * 用户评论 关联载入（一对多）
     */
    public function getVideoPlayComment(){
        return $this -> hasMany('app\video\model\VideoPlayComment','uid', 'uid');
    }
	
	/**
     * 用户评论回复 关联载入（一对多）
     */
    public function getVideoPlayCommentReplay(){
        return $this -> hasMany('app\video\model\VideoPlayCommentReplay','uid', 'uid');
    }
	
	/**
     * 用户评论回复 关联载入（一对多）
     */
    public function getErpAdminUser(){
        return $this -> hasMany('app\erp\model\ErpAdminUser','uid', 'uid');
    }
}
